// 定义一个混入对象
import { PAGINATION } from '@/common/config'
import { getFinalPageNum } from '@/utils/methods'
export const tableMixin = {
    data() {
        return {
            tableData: [],
            tableLoading: false,
            paging: PAGINATION(),
        }
    },
    computed: {},
    mounted() {
        this.renderTable()
    },
    methods: {
        handleTableChange(pagination) {
            this.paging = {
                ...pagination,
            }
            this.renderTable()
        },
        delSuccess(successMessage = '删除成功') {
            this.$message.success(successMessage)
            this.paging &&
                (this.paging.current = getFinalPageNum(this.paging.total, this.paging.current, this.paging.pageSize, 1))
            this.renderTable()
        },
    },
    watch: {},
}
export const simpleTableMixin = {
    methods: {
        updateTable(isReset = true) {
            if (isReset) {
                this.$refs.simpleTable.initData()
                return
            }
            this.$refs.simpleTable.renderTable()
        },
        delRow(delId) {
            this.$refs.simpleTable.del(delId)
        },
    },
}
export const tbModalMixin = {
    data() {
        return {
            confirmLoading: false,
        }
    },
    computed: {},
    props: {
        currRow: {
            required: false,
            type: [Object],
        },
        updateData: {
            required: true,
            type: Function,
        },
        getRef: {
            required: false,
            type: Function,
        },
    },
    mounted() {
        this.beforeShowModal()
    },
    beforeDestroy() {
        this.$refs.form && this.$refs.form.resetFields()
    },
    methods: {
        async handleOk() {
            console.log('handleOk')
            this.confirmLoading = true
            this.$refs.form.validate(async valid => {
                console.log(valid, 'valid')
                if (valid) {
                    const saveFunctionKey = 'save'
                    this[saveFunctionKey]()
                        .then(() => {
                            this.$message.success('操作成功')
                            this.RAISE_EVT(this.EVT_ENUM.FW_CLOSE_DIALOG)
                            this.updateData()
                        })
                        .catch(e => {
                            console.log(e)
                        })
                        .finally(() => {
                            this.confirmLoading = false
                        })
                } else {
                    this.confirmLoading = false
                    return false
                }
            })
        },
    },
    watch: {},
}
